﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

using PowerCreator.LMS.CMS.Entity;
using PowerCreator.LMS.Web.CMS;
using PowerCreator.LMS.Web.Course;
using PowerCreator.LMS.Common.Script;
public partial class sysadmin_NewManager_ManagerNew : PowerCreator.LMS.Web.Base.BasePage
{
    New objNew = new New();
    Course opCourse = new Course();
    NewType objT = new NewType();
    VOTNewType voT = new VOTNewType();
    private PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();
    private string url = "../login.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitPage();
            string search = GetSearch();
            BinderPager(search);
            BinderPG(search, 1);
            if (CheckUser())
            {
                btnAddNew.Enabled = true;
                btn_DelCheck.Enabled = true;
                btn_IsCheck.Enabled = true;
            }
            else
            {
                btnAddNew.Enabled = false;
                btn_DelCheck.Enabled = false;
                btn_IsCheck.Enabled = false;
            }
        }
    }


    private bool CheckUser()
    {
        bool rt = false;
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            Script.RedirectNewUrl(url);
        }
        else
        {
            login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
            if (login == null)
            {
                Script.RedirectNewUrl(url);
            }
            else
            {
                if (login.VOAdvancedUser == null)
                {
                    Script.RedirectNewUrl(url);
                }
                else if (login.VOAdvancedUser.AUserGroup == 1 || login.VOAdvancedUser.AUserGroup == 2 || login.VOAdvancedUser.AUserGroup == 3)
                {
                    rt = true;
                }
            }
        }

        return rt;

    }

    protected override void InitPage()
    {
        lbl_InfoTitle_ManagerNew.Text = GetLanageValueByKey("lab_ManagerNew_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_ManagerNew.Text = "新闻列表";
        btnAddNew.Text = GetLanageValueByKey("btn_AddNew", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnSearch.Text = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);

        chkAll.Text = GetLanageValueByKey("chkbox_ManagerQuestion_chkboxAll", PowerCreator.LMS.Language.LanagetItemType.Page);
        chkCancel.Text = GetLanageValueByKey("chkbox_ManagerQuestion_chkboxCancel", PowerCreator.LMS.Language.LanagetItemType.Page);

        btn_DelCheck.Text = GetLanageValueByKey("btnDel", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnAddNew.Text = GetLanageValueByKey("btn_AddNew", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_IsCheck.Text = GetLanageValueByKey("btnAuditing", PowerCreator.LMS.Language.LanagetItemType.Page);
        
        base.InitPage();

        //btnAddNew.Visible = false;
        //if (IsLicensed(PowerCreator.LMS.Action.Core.ActionInfo.AddJob))
        //    btnAddNew.Visible = true;

    }
    private string GetSearch()
    {
        string search = "";
        if (!string.IsNullOrEmpty(txtNewTitle.Text))
            search = string.Format("NewKeyword like '%{0}%' or NewTitle like '%{0}%' or  NewSubhead like '%{0}%' or  NewAuthor like '%{0}%'", txtNewTitle.Text.Trim());
        return search;
    }

    protected void BinderPager(string search)
    {
        AspNetPager1.RecordCount = objNew.Count(search);
        AspNetPager1.PageSize = LMSPageSize;
    }
    private void BinderPG(string search,int pageIndex)
    {
        string orderby = " NewID desc";
        DataTable dt = objNew.Query(search, orderby, LMSPageSize, pageIndex);
        dt.Columns.Add("CreateFile");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            dt.Rows[i]["CreateFile"] = "未生成";
            if (dt.Rows[i]["NewUrl"] != null)
            {
                if (dt.Rows[i]["NewUrl"].ToString().Length > 0)
                    dt.Rows[i]["CreateFile"] = "已生成";
            }
        }
        rptNew.DataSource = dt;
        rptNew.DataBind();

        PreAspNetPager(AspNetPager1);
    }
    protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                LinkButton lnkbtn_editor = (LinkButton)e.Item.FindControl("lnkbtn_editor");
                LinkButton lnkbtn_Ischeck = (LinkButton)e.Item.FindControl("lnkbtn_Ischeck");
                LinkButton lnkbtn_istop = (LinkButton)e.Item.FindControl("lnkbtn_istop");
                LinkButton lnkbtn_del = (LinkButton)e.Item.FindControl("lnkbtn_del");
                LinkButton lnkbtn_create = (LinkButton)e.Item.FindControl("lnkbtn_create");
                Label lab_NewTypeID = (Label)e.Item.FindControl("lab_NewTypeID");
                if (CheckUser())
                {
                    lnkbtn_editor.Enabled = true;
                    lnkbtn_Ischeck.Enabled = true;
                    lnkbtn_istop.Enabled = true;
                    lnkbtn_del.Enabled = true;
                    lnkbtn_create.Enabled = false;
                    try
                    {
                     voT = objT.Load(int.Parse(lab_NewTypeID.Text));
                     if (voT.FKNewTemlletID != null)
                     {
                         if(voT.FKNewTemlletID>0)
                             lnkbtn_create.Enabled = true;
                     }
                   }
                    catch(Exception ex)
                    {
                        lnkbtn_create.Enabled = false;
                    }
                    lnkbtn_del.Attributes.Add("onClick", "javascript:return confirm('确定删除此记录？');");
                }
                else
                {
                    lnkbtn_editor.Enabled = false;
                    lnkbtn_Ischeck.Enabled = false;
                    lnkbtn_istop.Enabled = false;
                    lnkbtn_create.Enabled = false;
                    lnkbtn_del.Enabled = false;
                }
            }
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderPager(search);
        BinderPG(search,1);
    }
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        string url = "AddNew.aspx";
        Script.RedirectURL(url);
    }
  
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderPG(search, AspNetPager1.CurrentPageIndex);
    }
    protected void rptNew_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        Label lbl_msg = (Label)e.Item.FindControl("lbl_msg");

        if (e.CommandName.ToLower() == "leditor")
        {
            //if (IsLicensed("NewsUpdate"))
            //{
                string url = "AddNew.aspx?NewID=" + e.CommandArgument.ToString();
                Script.RedirectURL(url);
            //}
            //else
            //{
            //    lbl_msg.Text = GetLanageValueByKey("msg_ManagerNew_checkmsg", PowerCreator.LMS.Language.LanagetItemType.Page);
            //}
        }
        else if (e.CommandName.ToLower() == "ldel")
        {
            //if (IsLicensed("NewsDel"))
            //{
                if (objNew.Del(int.Parse(e.CommandArgument.ToString())))
                {
                    string search = GetSearch();
                    BinderPager(search);
                    BinderPG(search, AspNetPager1.CurrentPageIndex);
                }
                else
                {
                    lbl_msg.Text = GetLanageValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page);
                }
            //}
            //else
            //{
            //    lbl_msg.Text = GetLanageValueByKey("msg_ManagerNew_checkmsg", PowerCreator.LMS.Language.LanagetItemType.Page);
            //}
        }
        else if (e.CommandName.ToLower() == "ischeck")
        {
            VOTNew vo = new VOTNew();
            vo = objNew.Load(int.Parse(e.CommandArgument.ToString()));
            if (vo.NewState == true)
                vo.NewState = false;
            else
            {
                vo.NewState = true;
                vo.NewPublishTime = DateTime.Now;
            }
            objNew.Update(vo);
            string search = GetSearch();
            BinderPager(search);
            BinderPG(search, AspNetPager1.CurrentPageIndex);
        }
        else if (e.CommandName.ToLower() == "istop")
        {
            VOTNew vo = new VOTNew();
            vo = objNew.Load(int.Parse(e.CommandArgument.ToString()));
            if (vo.NewIsTop == true)
                vo.NewIsTop = false;
            else
                vo.NewIsTop = true;
            objNew.Update(vo);
            string search = GetSearch();
            BinderPager(search);
            BinderPG(search, AspNetPager1.CurrentPageIndex);
        }
        else if (e.CommandName.ToLower() == "create")
        {
            CreateNewFile(int.Parse(e.CommandArgument.ToString()));
            string search = GetSearch();
            BinderPager(search);
            BinderPG(search, AspNetPager1.CurrentPageIndex);         
        }
    }

    private void CreateNewFile(int newid)
    {
        string fileurl = "";
        string newfileurl = "";

        //获取当前新闻模板文件路径
        VOTNew vo = new VOTNew();
        vo = objNew.Load(newid);
        if (vo.FKNewNewTypeID != null)
        {
            VOTNewTemplet voNT = new VOTNewTemplet();
            NewTemplet objNT = new NewTemplet();
            voNT = objNT.Load(vo.FKNewNewTypeID);
            if (voNT.Url != null)
                fileurl = voNT.Url;
        }

        try
        {

            if (fileurl.Length > 0)
            {
                string filePath = HttpContext.Current.Server.MapPath(fileurl);

                if (System.IO.File.Exists(filePath))
                {
                    string newfile = System.IO.File.ReadAllText(filePath, System.Text.Encoding.Default);
                    while (newfile.IndexOf("&Title&") > 0)
                        newfile = newfile.Replace("&Title&", vo.NewTitle);
                    while (newfile.IndexOf("&NewSubhead&") > 0)
                        newfile = newfile.Replace("&NewSubhead&", vo.NewSubhead);
                    while (newfile.IndexOf("&NewCreatorname&") > 0)
                        newfile = newfile.Replace("&NewCreatorname&", vo.NewCreatorname);
                    while (newfile.IndexOf("&TimePublish&") > 0)
                        newfile = newfile.Replace("&TimePublish&", vo.NewAddTime.ToString());
                    while (newfile.IndexOf("&NewSource&") > 0)
                        newfile = newfile.Replace("&NewSource&", vo.NewSource);
                    while (newfile.IndexOf("&NewAuthor&") > 0)
                        newfile = newfile.Replace("&NewAuthor&", vo.NewAuthor);
                    while (newfile.IndexOf("&NewKeyword&") > 0)
                        newfile = newfile.Replace("&NewKeyword&", vo.NewKeyword);
                    while (newfile.IndexOf("&NewPictureUrl&") > 0)
                        newfile = newfile.Replace("&NewPictureUrl&", vo.NewPictureUrl);
                    while (newfile.IndexOf("&NewContent&") > 0)
                        newfile = newfile.Replace("&NewContent&", vo.NewContent);

                    string newfilepath = "../../fileManager/Html/CreateNewFile/" + vo.FKNewNewTypeID + "/";

                    string filetype = ".html";
                    string fileName = opCourse.GetUploadResourceFileName(filetype);
                    newfileurl = newfilepath + fileName;
                    newfilepath = HttpContext.Current.Server.MapPath(newfilepath);
                    //如果路径不存在，则创建
                    if (System.IO.Directory.Exists(newfilepath) == false)
                    {
                        System.IO.Directory.CreateDirectory(newfilepath);
                    }
                    newfilepath = newfilepath + fileName;
                    System.IO.File.WriteAllText(newfilepath, newfile, System.Text.Encoding.Default);

                }

            }
            //}
        }
        catch (System.Exception ex)
        {
            Response.Write(ex);
            Response.End();

        }
        vo.NewUrl = newfileurl;
        objNew.Update(vo);
    }
    protected void chkAll_CheckedChanged(object sender, EventArgs e)
    {
        chkCancel.Checked = false;
        //全选 / 取消
        for (int i = 0; i < rptNew.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rptNew.Items[i].FindControl("Chbox_Check");
            if (chkAll.Checked)
                cbox.Checked = true;
            else
                cbox.Checked = false;
        }
    }
    protected void chkCancel_CheckedChanged(object sender, EventArgs e)
    {
        chkAll.Checked = false;
        //反选
        for (int i = 0; i < rptNew.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rptNew.Items[i].FindControl("Chbox_Check");
            if (cbox.Checked)
                cbox.Checked = false;
            else
                cbox.Checked = true;
        }
    }
    protected void btn_DelCheck_Click(object sender, EventArgs e)
    {
        try
        {
            for (int i = 0; i < rptNew.Items.Count; i++)
            {
                CheckBox cbox = (CheckBox)rptNew.Items[i].FindControl("Chbox_Check");
                Label lab_NewID = (Label)rptNew.Items[i].FindControl("lab_NewID");
                if (cbox.Checked)
                {
                    //if (IsLicensed("NewsDel"))
                    //{
                        if (objNew.Del(int.Parse(lab_NewID.Text)))
                        {
                            
                        }
                        else
                        {
                            Script.Alert(this, "msg", GetLanageValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page));
                        }
                    //}
                    //else
                    //{
                    //   Script.Alert(this, "msg", GetLanageValueByKey("msg_ManagerNew_checkmsg", PowerCreator.LMS.Language.LanagetItemType.Page));
                    //}
                }
            }
            string search = GetSearch();
            BinderPager(search);
            BinderPG(search, AspNetPager1.CurrentPageIndex);
            Script.Alert(this, "msg", GetLanageValueByKey("Msg_ManagerQuestion_WorkOK", PowerCreator.LMS.Language.LanagetItemType.Page));
        }
        catch
        {
            Script.Alert(this, "msg", GetLanageValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page));
        }
    }
    protected void btn_IsCheck_Click(object sender, EventArgs e)
    {
        try
        {
            for (int i = 0; i < rptNew.Items.Count; i++)
            {
                CheckBox cbox = (CheckBox)rptNew.Items[i].FindControl("Chbox_Check");
                Label lab_NewID = (Label)rptNew.Items[i].FindControl("lab_NewID");
                if (cbox.Checked)
                {
                    VOTNew vo = new VOTNew();
                    vo = objNew.Load(int.Parse(lab_NewID.Text));
                    if (vo.NewState == true)
                        vo.NewState = false;
                    else
                    {
                        vo.NewState = true;
                        vo.NewPublishTime = DateTime.Now;
                    }
                    objNew.Update(vo);
                    
                }
            }
            string search = GetSearch();
            BinderPager(search);
            BinderPG(search, AspNetPager1.CurrentPageIndex);
            Script.Alert(this, "msg", GetLanageValueByKey("Msg_ManagerQuestion_WorkOK", PowerCreator.LMS.Language.LanagetItemType.Page));
        }
        catch
        {
            Script.Alert(this, "msg", GetLanageValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page));
        }
    }
}

